
from mini_tools.mytools import *
import concurrent.futures as thddd



ms = sqlseverDB('{SQL Server}', SQL_SERVER, 'SuMaiTongPol', SQL_USER, SQL_PWD)

# 生成随机SKU ID
def generate_sku_id(length=10):
    characters = string.ascii_uppercase + string.digits
    sku_id = ''.join(random.choices(characters, k=length))
    return sku_id

qqkws=[]
# 上传SKU并执行插入数据库操作
def upload000(data):
    global qqkws
    itemid,pname=data
    flag=False

    if contains_word(pname,qqkws):
        flag=True
    
    aff=0
    if flag:
        aff=ms.ExecNoQuery('update proshopeeph set daochu=-5 where itemid=?',(itemid,))
        print(f'itemid:{itemid}的标题包含侵权词标记,标记行数:{aff}')

    return itemid,flag,aff


def pppp():
    global qqkws
    print(f'先获取当前印尼侵权词...')
    rs_qqc=ms.ExecQuery('select qid,qcontent from qqc where qtype=?',('印尼语普货',))
    qqkws=[r[1] for r in rs_qqc]
    print(f'当前共有 {len(qqkws)} 个印尼侵权词还没过滤')
    rs= ms.ExecQuery('select itemid,yntitle from proshopeeph where id in (select min(id) from proshopeeph where  datatype=1 and daochu>=0 and yntitle is not null group by itemid)')
    suc=0
    with thddd.ThreadPoolExecutor(max_workers=20) as t:

        tasks=[t.submit(upload000,r) for r in rs]
        task_count=len(tasks)
        print(f'共有 {task_count} 个印尼标题待过滤')
        for  t0 in thddd.as_completed(tasks):
            try:
                res=t0.result()
                if res[2]:
                    suc+=1
            except Exception as e:
                print(f'线程错误 => {e}')
            
            task_count-=1
            print(f'任务队列剩余:{task_count}')
    
    #wstr=','.join(['?' for _ in qids])
    # aff_qqc=ms.ExecNoQuery(f'update qqc set state=1 where qid in ({wstr})',qids)
    # print(f'过滤完毕,标记了 {suc} 个产品daochu为-1,将 {aff_qqc} 个侵权词state置1')


def upload_dp(shopid,datas):
    flag=False
    print(f'shopid:{shopid},共{len(datas)} 个产品')
    for data in datas:
        itemid,yntitle=data
        if 'kaos' in yntitle.lower():
            flag=True
            break
        
    aff=0
    if not flag:
        aff=ms.ExecNoQuery('update proshopeeph set ispoddp=-1 where shopid=?',(shopid,))
        print(f'shopid:{shopid}非pod店铺,标记店铺-1:行数:{aff}')

    return itemid,flag,aff

def up_pod_dp():

    rs= ms.ExecQuery('''select itemid,shopid,yntitle from proshopeeph 
                     where id in (select min(id) from proshopeeph
                     where yntitle is not null and datatype=2 and PingTai=? and ispoddp=0 and daochu!=-1 group by itemid)''',('菲律宾虾皮',))
    print(f'共计{len(rs)}个产品')
    rp_shop_data={}

    for itemid,shopid,yntitle in rs:

        if rp_shop_data.get(shopid):
            rp_shop_data[shopid].append([itemid,yntitle])
        else:
            rp_shop_data[shopid]=[[itemid,yntitle]]


    suc=0
    with thddd.ThreadPoolExecutor(max_workers=20) as t:

        tasks=[t.submit(upload_dp,shopid,r) for shopid,r in rp_shop_data.items()]
        task_count=len(tasks)
        print(f'共有 {task_count} 个店铺待过滤')
        for  t0 in thddd.as_completed(tasks):
            try:
                res=t0.result()
                if not res[2]:
                    suc+=1
            except Exception as e:
                print(f'线程错误 => {e}')
            
            task_count-=1
            print(f'任务队列剩余:{task_count}')
    
    print(f'总计更新:{suc}')

pppp()